package com.example.businessmanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.businessmanage.domain.dto.*;
import com.example.businessmanage.service.LendService;
import com.example.common.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 暂时外借控制器（主人操作：申请外借、选择代养人、申请取回等）
 */
@RestController
@RequestMapping("/api/v1/lend")
@Api(tags = "暂时外借管理接口（主人操作）")
@RequiredArgsConstructor
public class LendController {

    private final LendService lendService;

    @PostMapping("/apply")
    @ApiOperation("发起外借申请（主人操作，外借情况改为1-已申请外借）")
    public Boolean applyLend(@Validated @RequestBody LendApplyDTO lendApplyDTO) {
        // 从UserContext获取当前登录主人ID
        Long ownerUserId = UserContext.getUser();
        return lendService.applyLend(lendApplyDTO, ownerUserId);
    }

    @PostMapping("/select-foster")
    @ApiOperation("选择代养人（外借情况1或6时可用，改为2-已选择代养）")
    public Boolean selectFoster(@Validated @RequestBody SelectFosterDTO selectFosterDTO) {
        Long ownerUserId = UserContext.getUser();
        return lendService.selectFoster(selectFosterDTO, ownerUserId);
    }

    @PostMapping("/retrieve")
    @ApiOperation("申请取回宠物（根据当前状态改5或7）")
    public Boolean retrievePet(@Validated @RequestBody OwnerRetrieveDTO ownerRetrieveDTO) {
        Long ownerUserId = UserContext.getUser();
        return lendService.retrievePet(ownerRetrieveDTO, ownerUserId);
    }

    @GetMapping("/my-pet-foster")
    @ApiOperation("查询宠物的待选择代养人列表（分页）")
    public IPage<FosterUserRespDTO> getToBeSelectedFoster(
            @RequestParam Long petId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long ownerUserId = UserContext.getUser();
        return lendService.getToBeSelectedFoster(petId, ownerUserId, pageNum, pageSize);
    }
}
